home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 May / CHIP Mayıs 1997.iso / cont / web / winsock / netxray / data.1 / RIPCSV.BAS < prev    next >
Encoding:
BASIC Source File  |  1995-12-15  |  7.1 KB  |  301 lines

  1. '
  2. ' This Basic script demonstrates NetXRay ability to import
  3. ' IP Address Table file into the Address book.
  4. ' The imported file must contain the following fields
  5. '
  6. ' "Hostname","TCP/IP Address","Location","LAN Type","Full Name","LAN Address","Phone","Serial Number","Application","Model"
  7. '
  8. ' For Example
  9. '
  10. ' "receiver","124.45.103.67","Candle Stick","Ethernet","Rice, Jerry","0800203498ad","1123","555-1213","SOLARIS 2.3","SPARCSTATION 5"
  11. '
  12. ' An example of this file 'ipdbsamp.csv' is included in the release.
  13.  
  14. ' Basic Program Notes:
  15. '
  16. ' Note the new keyword "App" that replaces "Lib <libname>" in the Declare
  17. ' statement when the function resides in the App that is calling Enable
  18.  
  19. Declare Sub mcb App (ByVal ctx&, ByVal type%, ByVal errno%, ByVal str$)
  20. Declare Function OpenTextFile App (ByVal filename$) As Integer
  21. Declare Function CloseTextFile App (ByVal fid%) As Integer
  22. Declare Function ReadTextLine App (ByVal fid%, ByVal buf$) As Integer
  23. Declare Function MyGetOpenFilename App (ByVal filename$, ByVal fileExt$, ByVal fileInit$, ByVal filter$) As Integer
  24.  
  25. Dim g_linebuf$ As String * 256
  26. Dim g_buflen% As Integer
  27. Dim g_filename$ As String * 256
  28.  
  29. Sub Main ()
  30.     Dim fid% As Integer
  31.     Dim a_char$ As String
  32.     Dim buf$ As String
  33.     Dim appObj As object
  34.     Dim addrBookObj As object
  35.     Dim FileExt$ As String
  36.     Dim FileInitName$ As String
  37.     Dim FileFilter$ As String
  38.  
  39.     Set appObj = CreateObject("NetXRay.Application.1")
  40.     Set addrBookObj = appObj.GetAddressBookDoc()
  41.  
  42.     FileInitName$ = "*.csv"
  43.     FileFilter$ = "CSV (Comma delimited) | *.csv ||"
  44.     FileExt$ = "csv"
  45.  
  46.     bOpen% = MyGetOpenFilename(g_filename$, FileExt$, FileInitName$, FileFilter$)
  47.     If bOpen% = 0 Then
  48.         GoTo CancelOpen
  49.     End If
  50.  
  51.     fid% = OpenTextFile(g_filename$)
  52.     If fid% > 0 Then
  53.         Do
  54. NextLine:
  55.             g_buflen% = ReadTextLine(fid%, g_linebuf$)
  56.             If g_buflen% <= 0 Then        
  57.                 'Exit Do
  58.                 GoTo EndOfFile
  59.             End If
  60.  
  61.             buf$ = StripLeadingSpace(g_linebuf$)
  62.  
  63.             'not enough data
  64.             'g_buflen% = Len(buf$)
  65.             If Len(buf$) < 5 Then        
  66.                 GoTo NextLine
  67.             End If
  68.  
  69.             'skip the comment
  70.             a_char$ = Left$(buf$, 1)
  71.             If a_char$ = "#" Then        
  72.                 GoTo NextLine
  73.             End If
  74.  
  75.             'get the Host name'
  76.             HostName$ = GetDCSWord(buf$, 1)
  77.             If Len(HostName$) <= 2 Then
  78.                 GoTo NextLine
  79.             End If
  80.             HostName$ = StripQuote(HostName$)
  81.  
  82.             'get the IP token
  83.             IPaddr$ = GetDCSWord(buf$, 2)
  84.             If Len(IPaddr$) <= 2 Then
  85.                 IPaddr$ = "000.000.000.000"
  86.             Else
  87.                 IPaddr$ = StripQuote(IPaddr$)
  88.             End If
  89.  
  90.             'get the Location token
  91.             Location$ = GetDCSWord(buf$, 3)
  92.             Location$ = StripQuote(Location$)
  93.  
  94.             'get the LAN type token
  95.             LANtype$ = GetDCSWord(buf$, 4)
  96.             LANtype$ = StripQuote(LANtype$)
  97.  
  98.  
  99.             'get the LAN Address token
  100.             LANaddr$ = GetDCSWord(buf$, 6)
  101.             If Len(LANaddr$) <= 2 Then
  102.                 LANaddr$ = "000000000000"
  103.             Else
  104.                 LANaddr$ = StripQuote(LANaddr$)
  105.             End If
  106.  
  107.             bAddOK = addrBookObj.AddNewAddr(HostName$, LANaddr$, IPaddr$, LANtype$, Location$)
  108.  
  109.         Loop While g_buflen% > 0
  110.  
  111. EndOfFile:
  112.         CloseTextFile(fid%)
  113.     End If
  114.  
  115.     Response = MsgBox("Import file completed!", MB_OK, "NetXRay")
  116.  
  117. CancelOpen:
  118.     i = 0
  119. End Sub
  120.  
  121. '----------------------------------------------------------
  122. 'remove double or single quote chars from the token. Assuming quote is balanced
  123. Function StripQuote (Buf As String) As String
  124.     Dim a_char$, tabCh$
  125.     Dim temp$
  126.     temp$ = Buf
  127.     dquoteCh$ = Chr$(34)
  128.     squoteCh$ = Chr$(39)
  129.     If (Len(temp$) > 0) Then
  130.         a_char$ = Left$(temp$, 1)
  131.         If a_char$ = dquoteCh$ Or a_char$ = squoteCh$ Then
  132.             temp$ = Right$(temp$, Len(temp$) - 1)
  133.         End If
  134.     End If
  135.  
  136.     If (Len(temp$) > 0) Then
  137.         a_char$ = Right$(temp$, 1)
  138.         If a_char$ = dquoteCh$ Or a_char$ = squoteCh$ Then
  139.             temp$ = Left$(temp$, Len(temp$) - 1)
  140.         End If
  141.     End If
  142.     StripQuote = temp$
  143. End Function
  144.  
  145. '----------------------------------------------------------
  146. 'Counts the words in a string that are separated by ",(Double quote & comma)
  147. '
  148. Function CountDCSWords (S As String) As Integer
  149. '    Dim WC% As Integer, Pos% As Integer
  150.  
  151.     If Len(S) = 0 Then
  152.         CountDCSWords = 0
  153.         Exit Function
  154.     End If
  155.  
  156.     WC% = 1
  157.     Sperator$ = Chr$(34) + ","
  158.     Pos% = InStr(1, S, Sperator$)
  159.  
  160.     Do 
  161.         If Pos% > 0 Then
  162.             WC% = WC% + 1
  163.             Pos% = InStr(Pos% + 2, S, Sperator$)
  164.         Else
  165.             Exit Do
  166.         End If
  167.     Loop
  168.  
  169.     CountDCSWords = WC%
  170. End Function
  171.  
  172. '----------------------------------------------------------
  173. 'Returns the nth word in a specific field in a string that
  174. ' are separated by ",(Double quote & comma).
  175. '
  176. Function GetDCSWord (S As String, Indx As Integer) As String
  177.     Dim WC% As Integer, Count As Integer, SPos As Integer, EPos As Integer
  178.  
  179.     WC% = CountDCSWords(S)
  180.  
  181.     If Indx < 1 Or Indx > WC% Then
  182.         GetDCSWord = Null
  183.         Exit Function
  184.     End If
  185.  
  186.     Sperator$ = Chr$(34) + ","
  187.     Count = 1
  188.     SPos = 1
  189.     For Count = 2 To Indx
  190.       SPos = InStr(SPos, S, Sperator$) + 2
  191.     Next Count
  192.  
  193.     EPos = InStr(SPos, S, Sperator$)
  194.     If EPos <= 0 Then
  195.         EPos = Len(S)
  196.     End If
  197.  
  198.     GetDCSWord = Trim$(Mid$(S, SPos, EPos - SPos + 1))
  199. End Function
  200.  
  201. '----------------------------------------------------------
  202. 'Counts the words in a string that are separated by commas.
  203. '
  204. Function CountCSWords (S As String) As Integer
  205.     Dim WC% As Integer, Pos% As Integer
  206.  
  207.     If Len(S) = 0 Then
  208.         CountCSWords = 0
  209.         Exit Function
  210.     End If
  211.  
  212.     WC% = 1
  213.     Pos% = InStr(1, S, ",")
  214.  
  215.     Do 
  216.         If Pos% > 0 Then
  217.             WC% = WC% + 1
  218.             Pos% = InStr(Pos% + 1, S, ",")
  219.         Else
  220.             Exit Do
  221.         End If
  222.     Loop
  223.  
  224.     CountCSWords = WC%
  225. End Function
  226.  
  227. '----------------------------------------------------------
  228. 'Returns the nth word in a specific field.
  229. '
  230. Function GetCSWord (S As String, Indx As Integer) As String
  231.     Dim WC% As Integer, Count As Integer, SPos As Integer, EPos As Integer
  232.  
  233.     WC% = CountCSWords(S)
  234.  
  235.     If Indx < 1 Or Indx > WC% Then
  236.         GetCSWord = Null
  237.         Exit Function
  238.     End If
  239.  
  240.     Count = 1
  241.     SPos = 1
  242.     For Count = 2 To Indx
  243.       SPos = InStr(SPos, S, ",") + 1
  244.     Next Count
  245.  
  246.     EPos = InStr(SPos, S, ",") - 1
  247.     If EPos <= 0 Then
  248.         EPos = Len(S)
  249.     End If
  250.  
  251.     GetCSWord = Trim$(Mid$(S, SPos, EPos - SPos + 1))
  252. End Function
  253.  
  254. '----------------------------------------------------------
  255. 'Get a token
  256. Function GetToken (Buf$ As String) As String
  257.     Dim a_char$, tabCh$, crCh$, lfCh$
  258.     Dim temp$
  259.     tabCh$ = Chr$(9)
  260.     crCh$ = Chr$(13)
  261.     lfCh$ = Chr$(10)
  262.     commaCh$ = Chr$(44)
  263.     Pos = 1
  264.     buflen% = Len(Buf$)
  265.     For Pos = 1 To buflen%
  266.         a_char$ = Mid$(Buf$, Pos, 1)
  267. If a_char$ = " " Or a_char$ = tabCh$ Or a_char$ = crCh$ Or a_char$ = lfCh$ Or a_char$ = commaCh$ Then
  268.             GetToken = temp$
  269.             Exit Function
  270.         End If
  271.         If Pos = 1 Then
  272.             temp$ = a_char$
  273.         Else
  274.             temp$ = temp$ + a_char$
  275.         End If
  276.     Next Pos
  277.  
  278.     GetToken = temp$
  279. End Function
  280.  
  281. '----------------------------------------------------------
  282. 'remove the leading space, comma and tab chars
  283. Function StripLeadingSpace (Buf As String) As String
  284.     Dim a_char$, tabCh$
  285.     Dim temp$
  286.  
  287.     temp$ = Buf
  288.     tabCh$ = Chr$(9)
  289.     commaCh$ = Chr$(44)
  290.     While (Len(temp$) > 0)
  291.         a_char$ = Left$(temp$, 1)
  292.         If a_char$ <> " " And a_char$ <> tabCh$ And a_char$ <> commaCh$ Then
  293.             StripLeadingSpace = temp$
  294.             Exit Function
  295.         End If
  296.         temp$ = Right$(temp$, Len(temp$) - 1)
  297.     Wend
  298.  
  299.     StripLeadingSpace = temp$
  300. End Function
  301.